<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item label="校区编号">
              <a-input v-model="queryParam.xqbh"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="校区名称">
              <a-input v-model="queryParam.xqmc"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button type="primary" @click="openAdd">添加</a-button>
              <a-button type="primary" @click="batchDelete">批量删除</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      :pagination="{ pageSize: 10 }"
    >
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>

      <template slot="operation" slot-scope="a,b">
        <a @click="openEdit(b)">修改</a>
      </template>
    </s-table>

    <campus-edit
      ref="campusEdit"
      :visible.sync="visible"
      :editId.sync="editId"
      @cancel="visible = false"
      @create="handleCreate"
    />
  </a-card>
</template>

<script>
import { STable } from '~'
import campusEdit from './campusEdit'

export default {
  name: 'TableList',
  components: {
    STable,
    campusEdit
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: true,
      // 查询参数
      queryParam: {},
      postParams: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '校区编号',
          dataIndex: 'xqbh'
        },
        {
          title: '校区名称',
          dataIndex: 'xqmc'
        },
        {
          title: '备注',
          dataIndex: 'note'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          width: '150px'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.base.campusList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true, // 是否显示alert，已选中
      visible: false,
      confirmLoading: false,
      editId: null
    }
  },
  created () {
  },
  methods: {
    handleCreate () {
      const form = this.$refs.campusEdit.form
      form.validateFields((err, values) => {
        if (err) {
          return
        }
        if (this.editId) {
          this.$api.base.campusUpdate(Object.assign({id: this.editId}, values)).then(res => {
            this.$message.success('修改成功')
            this.$refs.table.refresh(false)
          })
        } else {
          this.$api.base.campusAdd(values).then((res) => {
            this.$message.success('添加成功')
            this.$refs.table.refresh(true)
          })
        }
        form.resetFields()
        this.visible = false
      })
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的校区')
        return
      }
      this.$confirm({
        title: '确定删除所选中的校区信息?',
        content: '当您点击确定按钮后，这些记录将会被彻底删除',
        centered: true,
        onOk: () => {
          let campusIds = []
          this.selectedRows.forEach(campus => {
            campusIds.push(campus.id)
          })
          this.$api.base.campusDelete(campusIds.join(',')).then(() => {
            this.$message.success('删除成功')
            this.selectedRowKeys = []
            this.selectedRows = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => {
        }
      })
      console.log(this.selectedRowKeys)
      console.log(this.selectedRows)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    openAdd () {
      this.editId = null
      this.visible = true
      this.$refs.campusEdit.form.resetFields()
    },
    openEdit (campus) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.campusEdit.form.setFieldsValue({xqbh: campus.xqbh, xqmc: campus.xqmc, note: campus.note})
        this.editId = campus.id
      })
    }
  }
}
</script>
